package com.java.Servlet;

import com.java.Exception.MyException;
import com.java.entity.AirLine;
import com.java.entity.FlightInformation;
import com.java.service.*;
import com.java.service.Impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/Flight")
public class Flight extends HttpServlet {


    private AddFlightService flightService=new AddFlightImpl();
    private addLineService addlineservice=new addLineServiceImpl();
    private selectAllAirLineService selectAirLine=new selectAllAirLineIServicempl();
    private queryFlightByNoService queryFlightNoService=new queryFlightByNoImpl();
    private queryAirLineService queryAirLine=new queryAirLineServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws  ServletException, IOException{
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String mark = req.getParameter("mark");
        if ("addLine".equals(mark)){
            this.doAddLine(req,resp);
        }else if ("addFlight".equals(mark)){
            this.doAddFlight(req,resp);
        }else if ("selectAllAirLine".equals(mark)){
            this.doselectAllLine(req,resp);
        }else if ("selectAllFlightByNo".equals(mark)){
            this.doselectFlight(req,resp);
        }else if ("addFlightByNo".equals(mark)){
            this.doAddFlightByNo(req,resp);
        }

    }

    private void doAddFlightByNo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String airlineNo = req.getParameter("airlineNo");
        try {
            AirLine airLine1 = queryAirLine.queryAirLineByNo(airlineNo);
            String airlineNo1 = airLine1.getAirlineNo();
            String origin = airLine1.getOrigin();
            String destination = airLine1.getDestination();
            float distance = airLine1.getDistance();
            AirLine airLine=new AirLine();
            airLine.setAirlineNo(airlineNo1);
            airLine.setOrigin(origin);
            airLine.setDestination(destination);
            airLine.setDistance(distance);
            req.setAttribute("airLine",airLine);
            req.getRequestDispatcher("/admin/addFlight.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据航线号查询该航线的航班
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doselectFlight(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String airlineNo = req.getParameter("airlineNo");
        try {
            List<FlightInformation> Flight = queryFlightNoService.queryFlightByNo(airlineNo);
            if (Flight.size()>0){
                req.setAttribute("Flight",Flight);
                req.getRequestDispatcher("/admin/flightList.jsp").forward(req,resp);
            }else {
                AirLine airLine = queryAirLine.queryAirLineByNo(airlineNo);
                req.setAttribute("airLine",airLine);
                req.getRequestDispatcher("/admin/addFlight.jsp").forward(req,resp);
//                req.setAttribute("kong","该航线暂未开通航班，请去添加航班");
//                req.getRequestDispatcher("/nullFlight.jsp").forward(req,resp);
            }
        } catch (MyException e) {
            e.printStackTrace();
            req.setAttribute("kong",e.getMessage());
            req.getRequestDispatcher("/nullFlight.jsp").forward(req,resp);
        }catch (SQLException e) {
            e.printStackTrace();
        }

    }


    /**
     * 查询所有的航线  航线列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doselectAllLine(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        try {
            List<AirLine> airLines = selectAirLine.queryAirLine(null);
            req.setAttribute("airLines",airLines);
            req.getRequestDispatcher("/admin/AirLineList.jsp").forward(req,resp);
        } catch (SQLException | MyException e) {
            e.printStackTrace();
        }


    }


    /**
     * 添加航线
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doAddLine(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //1.从前端获取传过来的值
        //添加航线前查询是否有该航线，如果没有，则先添加该航线
        String message="";
        String airlineNo = req.getParameter("airlineNo");
        try {
            PrintWriter out=resp.getWriter();
            AirLine airLine = queryAirLine.queryAirLineByNo(airlineNo);
            if (airLine !=null){
                message="该航线号已被用，请选择其他航线号";
                out.write(message);
                out.flush();
                out.close();

                this.doselectAllLine(req,resp);
                //req.getRequestDispatcher("/admin/AirLineList.jsp").forward(req,resp);
                return;
            }
            String origin = req.getParameter("origin");
            String destination = req.getParameter("destination");
            String distance = req.getParameter("distance");

            AirLine airLine1=new AirLine();//实例化对象
            //2.把获取的值给对象赋值
            airLine1.setAirlineNo(airlineNo);
            airLine1.setOrigin(origin);
            airLine1.setDestination(destination);
            airLine1.setDistance(Float.parseFloat(distance));
            //3.调用service层
            boolean flag = addlineservice.addLine(airLine1);
            if(flag){
                this.doselectAllLine(req,resp);
            }


        }catch (MyException e) {
            e.printStackTrace();
            req.setAttribute("msg",e.getMessage());

            req.getRequestDispatcher("/admin/addLine.jsp").forward(req,resp);
        }catch (SQLException e) {
            e.printStackTrace();
        }


    }


    /**
     * 添加航班
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doAddFlight(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String airlineNo = req.getParameter("airlineNo");
        //添加航班前的操作，查询是否有该航线，如果没有，则先添加该航线
        try {
            AirLine airLine = queryAirLine.queryAirLineByNo(airlineNo);
            if (airLine ==null){
                req.setAttribute("nullAirLine","该航线暂未开通，请先开通该航线");
                req.getRequestDispatcher("/nullAirList.jsp").forward(req,resp);

            }
        } catch (MyException  e) {
            e.printStackTrace();
            req.setAttribute("nullAirLine",e.getMessage());
            req.getRequestDispatcher("/nullAirList.jsp").forward(req,resp);
        }catch (SQLException  e) {
            e.printStackTrace();
            req.setAttribute("adminMessage","添加失败");
            req.getRequestDispatcher("/admin/flight.jsp").forward(req,resp);
        }


        String flightNo = req.getParameter("FlightNo");
        String origin = req.getParameter("origin");
        String destination = req.getParameter("destination");
        String leaveTime = req.getParameter("leaveTime");
        String arriveTime = req.getParameter("arriveTime");
        String totalSeats = req.getParameter("totalSeats");
        String price = req.getParameter("price");

        String leave = req.getParameter("leave");
        String arrive = req.getParameter("arrive");
        try {
            AirLine airLine = queryAirLine.queryAirLineByNo(airlineNo);

            //实例化对象
            FlightInformation fi=new FlightInformation();
            fi.setDistance(Float.toString(airLine.getDistance()));
            fi.setOrigin(airLine.getOrigin());
            fi.setDestination(airLine.getDestination());
            fi.setAirlineNo(airlineNo);
            fi.setFlightNo(flightNo);
            fi.setOrigin(airLine.getOrigin());
            fi.setDestination(airLine.getDestination());
            fi.setLeaveTime(leaveTime);
            fi.setArriveTime(arriveTime);
            fi.setTotalSeats(Integer.parseInt(totalSeats));
            fi.setPrice(price);
            fi.setLeave(leave);
            fi.setArrive(arrive);

            try {
                boolean flag = flightService.addFlight(fi);
                if (flag){
                    List<FlightInformation> Flight = queryFlightNoService.queryFlightByNo(airlineNo);
                    if (Flight.size()>0){
                        req.setAttribute("Flight",Flight);
                        req.getRequestDispatcher("/admin/flightList.jsp").forward(req,resp);
                    }

                }
            } catch (SQLException | MyException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }


    }
}
